$(document).ready(function() {
	InitForm();
	InitTitle();
})
function setcookie(name, value, iDay) {
	var oDate = new Date();
	oDate.setDate(oDate.getDate() + iDay);
	document.cookie = name + "=" + value + ";expires=" + oDate + "; path=/";
}
function InitTitle() {
	var type = GetQueryString("type");
	if(type == "add") {
		message = "添加新图片";
	} else {
		message = "编辑新图片";
	}
	var Main = new Vue({
		el: '.add_title',
		data: {
			title_message: message
		}
	})
}
function InitForm() {
	var isnew=0;//默认为新增
	var type = GetQueryString("type");
	var fileListdata = [];
	var listdata = {};
	var showStaue = false;
	if(type == "add") {
		formdata = {
			title: '',
			subtitle: '',
			content: '',
			imgUrl: '',
			imgUrl2: ''
		}
	} else {
		var formdata = {};
		
		var id = GetQueryString("id");
		isnew=1;//代表修改,显示已上传的图片
		$.ajax({
			type: 'get',
			async: false,
			url: "/partner/getbyid",
			data: {
				'id': id
			},
			success: function(datas) {
				var data_json = eval('(' + datas + ')');
				formdata = data_json;
				listdata.url = data_json.imgUrl;
			}
		});
	}
	fileListdata.push(listdata);
	var Main = {
		data() {
			return {
				form: formdata,
				fileList: fileListdata,
				Previewdata:showStaue,
				isnew:isnew//0代表新增，1代表修改
			}
		},
		methods: {
			Cancle() {
				window.location.href = "/CooperativePartner";
			},
			upload: function(response, file, fileList) {
				this.form.imgUrl = response.msg;
			},
			handleRemove(file, fileList) {
		        this.form.imgUrl = "";
		    },
		    handlePreview(file){
				this.Previewdata = true;
			},
			ConfirmForm(formName) {
				this.$refs[formName].validate((valid) => {
					if(valid) {
						if(type == "add") {
							$.ajax({
								type: 'post',
								url: "/partner/add",
								data: this.form,
								success: function(data) {
									var printjson = JSON.stringify(data);
								}
							});
						} else {
							$.ajax({
								type: 'post',
								url: "/partner/update?id=" + GetQueryString("id"),
								data: this.form,
								success: function(data) {
									var printjson = JSON.stringify(data);
								}
							});
						}
						window.location.href = '/JumpPage';
						setcookie("key", "operate");
					} else {
						console.log('error submit!!');
						return false;
					}
				})
			}
		}
	}
	var Ctor = Vue.extend(Main)
	new Ctor().$mount('#app')
}

function GetQueryString(parameterName, url) {
	var str = location.search;
	if(url != undefined && url != null && typeof(url) != "undefined") {
		str = url;
	}

	var result = str.match(new RegExp("[\?\&]" + parameterName + "=([^\&]+)", "i"));
	if(result == null || result.length == 0) {
		return "";
	} else {
		return result[1];
	}
}